#!/bin/sh
echo "环境安装中...."
/usr/node/init.sh
echo "环境安装完成...."
crond -l 9
if [ -z $num ];then
	num=1
fi
if [ ! -d "/kuaicdn/app/ipes/" ];then
	echo "检测到猕猴桃程序尚未安装"
	echo "猕猴桃程序安装中..."
	_url=""
	arch=$(uname -m)
	if [[ $arch = "x86_64" ]]; then
		_url=https://ipes-tus.iqiyi.com/update/ipes-linux-amd64-llc-latest.tar.gz
	fi
	if [[ $arch = "aarch64" ]]; then
		_url=https://ipes-tus.iqiyi.com/update/ipes-linux-arm64-llc-latest.tar.gz
	fi
	if [[ $arch = "armv7l" ]]; then
		_url=https://ipes-tus.iqiyi.com/update/ipes-linux-arm-llc-latest.tar.gz
	fi
	mkdir /kuaicdn/res #创建目录
	_file_path=/kuaicdn/res/ipes-latest.tar.gz
	curl -Lo $_file_path $_url # 执行下载
	tar -zxvf $_file_path -C /kuaicdn/app #解压程序到指定目录
	echo "args:" > /kuaicdn/app/ipes/var/db/ipes/happ-conf/custom.yml
	for i in `seq $num`
	do
	echo "  - '/mnts$i/'" >> /kuaicdn/app/ipes/var/db/ipes/happ-conf/custom.yml
	done
	echo "猕猴桃程序安装完成..."
	./ipes/bin/ipes start
else
	echo "检测到猕猴桃程序已经安装"
fi
echo ""
echo  "###############################QQ群：970772910###################################################"
echo  "请访问下面地址注册登录"
echo  "http://www.bdkjcdn.com/transfer?cct=16244&cce=2ad237e649ebd3245bba39f56aef7543"
for j in $(seq 10)
do 
	if [ -f "/mnts1/happ/happ.0/hdata/config/infos.json" ];then
		echo  "请在上面网站登录后->机器提交->猕猴桃->中绑定以下你的设备ID："
		grep "client_id" /mnts$j/happ/happ.0/hdata/config/infos.json
		echo  "请在上面网站登录后->机器监控->注册ipes_sn->中绑定以下你的ipes_sn："
		grep "sn" /mnts$j/happ/happ.0/hdata/config/infos.json
		echo  "正在获取端口"
		echo "请将下面端口进行tcp+udp端口转发：（ip填物理机ip或容器ip）"
		for i in `seq $num`
		do
			grep "udp_port" /mnts$i/happ/happ.0/hdata/config/infos.json
		done
		break
	else
		echo "正在获取clientID与端口号中..."
		sleep 10
		if [ j == 10 ];then
			echo "获取clientID与端口失败，可能某端口被占用导致程序无法正常运行。"
		fi
	fi
done
/bin/sh